home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / jcl / jcl_unicode.h < prev    next >
C/C++ Source or Header  |  1999-12-09  |  4KB  |  146 lines

  1. // $Id: jcl_unicode.h,v 1.2 1999/12/09 18:02:26 lord Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #ifndef jcl_unicode_INCLUDED
  11. #define jcl_unicode_INCLUDED
  12.  
  13. #include <stdio.h>
  14. #include <iostream.h>
  15. #include <ctype.h>
  16. #include <wchar.h>
  17.  
  18. //TODO: remove this
  19. #define cerr cout
  20.  
  21. class Unicode
  22. {
  23. public:
  24.  
  25.     static inline int Getc(FILE *srcfile)
  26.     {
  27.         int ch = getc(srcfile);
  28.  
  29.         if (ch == L'\\')
  30.         {
  31.             int c = getc(srcfile);
  32.  
  33.             if (c == 'u')
  34.             {
  35.                 while (c == 'u')
  36.                     c = getc(srcfile);
  37.                 ch = 0;
  38.                 int i;
  39.                 for (i = 0; isxdigit(c) && i < 4; i++)
  40.                 {
  41.                     int multiplier[4] = {4096, 256, 16, 1};
  42.  
  43.                     switch(c)
  44.                     {
  45.                         case 'a': case 'A':
  46.                             ch += (10 * multiplier[i]);
  47.                             break;
  48.                         case 'b': case 'B':
  49.                             ch += (11 * multiplier[i]);
  50.                             break;
  51.                         case 'c': case 'C':
  52.                             ch += (12 * multiplier[i]);
  53.                             break;
  54.                         case 'd': case 'D':
  55.                             ch += (13 * multiplier[i]);
  56.                             break;
  57.                         case 'e': case 'E':
  58.                             ch += (14 * multiplier[i]);
  59.                             break;
  60.                         case 'f': case 'F':
  61.                             ch += (15 * multiplier[i]);
  62.                             break;
  63.                         default:
  64.                             ch += ((c - '0') * multiplier[i]);
  65.                     }
  66.                     c = getc(srcfile);
  67.                 }
  68.                 if (i != 4)
  69.                     /* ISSUE AN ERROR ABOUT BAD UNICODE CHARACTER !!! */ ;
  70.             }
  71.     
  72.             ungetc(c, srcfile);
  73.         }
  74.         else if (ch == L'\r')
  75.         {
  76.             ch = L'\n';
  77.             int c = getc(srcfile);
  78.             if (c != L'\n')
  79.                 ungetc(c, srcfile);
  80.         }
  81.  
  82.         return ch;
  83.     }
  84.  
  85.     //
  86.     // Read max_size Ascii characters from srcfile, convert them to unicode
  87.     // store them in block and return the number of unicode characters read.
  88.     //
  89.     static inline size_t read_file(FILE *srcfile, wchar_t *block)
  90.     {
  91.         wchar_t *p = block;
  92.         int c;
  93.  
  94.         while((c = Getc(srcfile)) != EOF)
  95.             *p++ = c;
  96.  
  97.         return p - block;
  98.     }
  99.  
  100.     //
  101.     // This function takes as arguments a pointer to a wchar_t string
  102.     // buffer: block,  and an integer: max_size. It attempts to read the
  103.     // next max_size Unicode characters from the input file into block (if
  104.     // the input file has at least that many characters left). It returns
  105.     // an integer which indicates how many Unicode characters were actually
  106.     // read.
  107.     //
  108.     static size_t read_block(FILE *srcfile, wchar_t *block, size_t max_size)
  109.     {
  110.         int n;
  111.         for (n = 0; n < max_size; n++)
  112.         {
  113.             int ch = Getc(srcfile);
  114.             if (ch == EOF)
  115.                 break;
  116.             *block++ = ch;
  117.         }
  118.     
  119.         return n;
  120.     }
  121.  
  122.     static inline void Cout(wchar_t ch)
  123.     {
  124.         cout << (ch > 0 && ch < 0x00C0 ? (char) ch : ' ');
  125.     }
  126.  
  127.     static inline void Cout(wchar_t *str)
  128.     {
  129.         for (; *str; str++)
  130.             Cout(*str);
  131.     }
  132.  
  133.     static inline void Cerr(wchar_t ch)
  134.     {
  135.         cerr << (ch > 0 && ch < 0x00C0 ? (char) ch : ' ');
  136.     }
  137.  
  138.     static inline void Cerr(wchar_t *str)
  139.     {
  140.         for (; *str; str++)
  141.             Cerr(*str);
  142.     }
  143. };
  144.  
  145. #endif
  146.